package com.bw.shop_2302.login

import android.app.Notification
import android.app.NotificationManager
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.mvvm.BaseActivity
import com.bw.base.net.Cons
import com.bw.shop_2302.R
import com.bw.shop_2302.databinding.ActivityLoginCodeBinding
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
@Route(path = "/app/LoginCodeActivity")
class LoginCodeActivity : BaseActivity<ActivityLoginCodeBinding,LoginViewModel>() {

    override fun initView() {

        binding.tvGologin.setOnClickListener {

            ARouter.getInstance().build("/app/UserLoginActivity").navigation()

        }

        binding.btSendcode.setOnClickListener {

            val phone = binding.etPhone.text.toString()
            if(phone.isEmpty()){
                ToastUtils.showLong("手机号不能为空")
                return@setOnClickListener
            }

            if(!RegexUtils.isMobileSimple(phone)){
                ToastUtils.showLong("手机号格式不正确")
                return@setOnClickListener
            }

            viewModel.sendcode(phone)

            binding.btSendcode.isEnabled=false

            lifecycleScope.launch {
                for (i in 15 downTo 1){

                    binding.btSendcode.text="$i 秒后获取"
                    delay(1000)
                    if(i==1){
                        binding.btSendcode.text="获取验证码"
                        binding.btSendcode.isEnabled=true
                    }
                }
            }

        }

        binding.btnLogin.setOnClickListener {

            val phone = binding.etPhone.text.toString()
            val code = binding.etCode.text.toString()

            if(phone.isEmpty()||code.isEmpty()){
                ToastUtils.showLong("手机号和验证码均不能为空")
                return@setOnClickListener
            }

            if(!RegexUtils.isMobileSimple(phone)){
                ToastUtils.showLong("手机号格式不正确")
                return@setOnClickListener
            }

            if(!binding.cbFwb.isChecked){
                ToastUtils.showLong("请先勾选同意协议")
                return@setOnClickListener
            }

            viewModel.LoginCode(binding.etCode.text.toString(),binding.etPhone.text.toString())

        }

    }

    override fun initData() {

        viewModel.sendsuccess.observe(this){

            if(it.code==200){
                //发送通知 //通知管理器 // 通知对象 //发送通知

                //通知管理器
                val manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager

                //通知对象
                val build = Notification.Builder(this)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle("验证码")
                    .setContentText("验证码: ${it.data.content.substring(8, 14)}")
                    .build()

                //发送通知
                manager.notify(1,build)

            }
            ToastUtils.showLong(it.message)

        }

        viewModel.LoginCodesuccess.observe(this){

            if(it.code==200){

                ToastUtils.showLong(it.message)
                SPUtils.getInstance().put(Cons.SP_TOKEN,it.data.token)
                SPUtils.getInstance().put(Cons.SP_PHONE,it.data.phone)
                finish()
                ARouter.getInstance().build("/app/User/UserActivity").navigation()
                //startActivity(Intent(this,UserActivity::class.java))

            }
            ToastUtils.showLong(it.message)
        }

        viewModel.LoginCodefail.observe(this){
            ToastUtils.showLong(it)
        }


    }

    override fun getviewmodel(): Class<LoginViewModel> =LoginViewModel::class.java

    override fun getLayoutID(): Int = R.layout.activity_login_code
}